<?php

/**
 * This is the model class for table "tanai_product_master".
 *
 * The followings are the available columns in table 'tanai_product_master':
 * @property string $ref
 * @property string $collection
 * @property string $collection_sub
 * @property double $grams
 * @property double $price_thb
 * @property string $ring_for
 * @property string $comments
 * @property string $p_size
 * @property string $color
 * @property string $detail_new
 * @property string $colour
 * @property string $metal
 * @property string $surface
 * @property string $stone
 * @property string $material
 * @property string $hallmark
 * @property string $comment
 * @property double $diameter
 * @property string $diameter_unit
 * @property double $length
 * @property string $length_unit
 * @property double $wide
 * @property string $wide_unit
 * @property double $thick
 * @property string $thick_unit
 * @property string $watch
 * @property string $watch_band
 * @property string $watch_movement
 * @property string $watch_waterproof
 * @property string $watch_case
 * @property double $watch_gram
 * @property double $stock_not_ring
 * @property double $stock_ring_freesize
 * @property double $stock_ring_size_2
 * @property double $stock_ring_size_3
 * @property double $stock_ring_size_4
 * @property double $stock_ring_size_5
 * @property double $stock_ring_size_6
 * @property double $stock_ring_size_7
 * @property double $stock_ring_size_8
 * @property double $stock_ring_size_9
 * @property double $stock_ring_size_10
 * @property double $stock_ring_size_11
 * @property double $stock_ring_size_12
 * @property string $entry_date
 */
class TanaiProductMaster extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return TanaiProductMaster the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tanai_product_master';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('ref', 'required'),
			array('grams, price_thb, diameter, length, wide, thick, watch_gram, stock_not_ring, stock_ring_freesize, stock_ring_size_2, stock_ring_size_3, stock_ring_size_4, stock_ring_size_5, stock_ring_size_6, stock_ring_size_7, stock_ring_size_8, stock_ring_size_9, stock_ring_size_10, stock_ring_size_11, stock_ring_size_12', 'numerical'),
			array('ref, collection_sub, colour, metal, surface, stone, material, watch_band, watch_movement, watch_case', 'length', 'max'=>150),
			array('collection, comments, comment', 'length', 'max'=>255),
			array('ring_for, diameter_unit, length_unit, wide_unit, thick_unit', 'length', 'max'=>15),
			array('p_size', 'length', 'max'=>180),
			array('color', 'length', 'max'=>60),
			array('detail_new, hallmark, watch, watch_waterproof', 'length', 'max'=>3),
			array('entry_date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('ref, collection, collection_sub, grams, price_thb, ring_for, comments, p_size, color, detail_new, colour, metal, surface, stone, material, hallmark, comment, diameter, diameter_unit, length, length_unit, wide, wide_unit, thick, thick_unit, watch, watch_band, watch_movement, watch_waterproof, watch_case, watch_gram, stock_not_ring, stock_ring_freesize, stock_ring_size_2, stock_ring_size_3, stock_ring_size_4, stock_ring_size_5, stock_ring_size_6, stock_ring_size_7, stock_ring_size_8, stock_ring_size_9, stock_ring_size_10, stock_ring_size_11, stock_ring_size_12, entry_date', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}
        
        public static function getCategoryName($key=null)
        {
            if($key!==null)
                $cat = Category::model()->findByAttributes(array('tag'=>$key))->name;//TODO ver si se puede hacer mas facil sacandolo de la relacion
                if($cat === null) {
                    return $key;
                } else {
                    return $cat;
                }
            return 'No existe';
        }
        
        public static function getSubcategoryName($key=null)
        {
            if($key!==null) {
                $subcat = Subcategory::model()->findByAttributes(array('tag'=>$key))->name;//TODO ver si se puede hacer mas facil sacandolo de la relacion
                if($subcat === null) {
                    return $key;
                } else {
                    return $subcat;
                }
            }
                
            return 'No existe';
        }

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'ref' => 'Código',
			'collection' => 'Categoría',
			'collection_sub' => 'Subcategoría',
			'grams' => 'Peso (g)',
			'price_thb' => 'Costo (THB)',
			'ring_for' => 'Anillo para',
			'comments' => 'Comentarios',
			'p_size' => 'Tamaño',
			'color' => 'Color',
			'detail_new' => 'Detail New',
			'colour' => 'Colour',
			'metal' => 'Metal',
			'surface' => 'Surface',
			'stone' => 'Piedra',
			'material' => 'Material',
			'hallmark' => 'Hallmark',
			'comment' => 'Comentarios',
			'diameter' => 'Diameter',
			'diameter_unit' => 'Diameter Unit',
			'length' => 'Largo',
			'length_unit' => 'Length Unit',
			'wide' => 'Wide',
			'wide_unit' => 'Wide Unit',
			'thick' => 'Thick',
			'thick_unit' => 'Thick Unit',
			'watch' => 'Watch',
			'watch_band' => 'Watch Band',
			'watch_movement' => 'Watch Movement',
			'watch_waterproof' => 'Watch Waterproof',
			'watch_case' => 'Watch Case',
			'watch_gram' => 'Watch Gram',
			'stock_not_ring' => 'Stock Not Ring',
			'stock_ring_freesize' => 'Stock Ring Freesize',
			'stock_ring_size_2' => 'Stock Ring Size 2',
			'stock_ring_size_3' => 'Stock Ring Size 3',
			'stock_ring_size_4' => 'Stock Ring Size 4',
			'stock_ring_size_5' => 'Stock Ring Size 5',
			'stock_ring_size_6' => 'Stock Ring Size 6',
			'stock_ring_size_7' => 'Stock Ring Size 7',
			'stock_ring_size_8' => 'Stock Ring Size 8',
			'stock_ring_size_9' => 'Stock Ring Size 9',
			'stock_ring_size_10' => 'Stock Ring Size 10',
			'stock_ring_size_11' => 'Stock Ring Size 11',
			'stock_ring_size_12' => 'Stock Ring Size 12',
			'entry_date' => 'Entry Date',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('ref',$this->ref,true);
		$criteria->compare('collection',$this->collection,true);
		$criteria->compare('collection_sub',$this->collection_sub,true);
		$criteria->compare('grams',$this->grams);
		$criteria->compare('price_thb',$this->price_thb);
		$criteria->compare('ring_for',$this->ring_for,true);
		$criteria->compare('comments',$this->comments,true);
		$criteria->compare('p_size',$this->p_size,true);
		$criteria->compare('color',$this->color,true);
		$criteria->compare('detail_new',$this->detail_new,true);
		$criteria->compare('colour',$this->colour,true);
		$criteria->compare('metal',$this->metal,true);
		$criteria->compare('surface',$this->surface,true);
		$criteria->compare('stone',$this->stone,true);
		$criteria->compare('material',$this->material,true);
		$criteria->compare('hallmark',$this->hallmark,true);
		$criteria->compare('comment',$this->comment,true);
		$criteria->compare('diameter',$this->diameter);
		$criteria->compare('diameter_unit',$this->diameter_unit,true);
		$criteria->compare('length',$this->length);
		$criteria->compare('length_unit',$this->length_unit,true);
		$criteria->compare('wide',$this->wide);
		$criteria->compare('wide_unit',$this->wide_unit,true);
		$criteria->compare('thick',$this->thick);
		$criteria->compare('thick_unit',$this->thick_unit,true);
		$criteria->compare('watch',$this->watch,true);
		$criteria->compare('watch_band',$this->watch_band,true);
		$criteria->compare('watch_movement',$this->watch_movement,true);
		$criteria->compare('watch_waterproof',$this->watch_waterproof,true);
		$criteria->compare('watch_case',$this->watch_case,true);
		$criteria->compare('watch_gram',$this->watch_gram);
		$criteria->compare('stock_not_ring',$this->stock_not_ring);
		$criteria->compare('stock_ring_freesize',$this->stock_ring_freesize);
		$criteria->compare('stock_ring_size_2',$this->stock_ring_size_2);
		$criteria->compare('stock_ring_size_3',$this->stock_ring_size_3);
		$criteria->compare('stock_ring_size_4',$this->stock_ring_size_4);
		$criteria->compare('stock_ring_size_5',$this->stock_ring_size_5);
		$criteria->compare('stock_ring_size_6',$this->stock_ring_size_6);
		$criteria->compare('stock_ring_size_7',$this->stock_ring_size_7);
		$criteria->compare('stock_ring_size_8',$this->stock_ring_size_8);
		$criteria->compare('stock_ring_size_9',$this->stock_ring_size_9);
		$criteria->compare('stock_ring_size_10',$this->stock_ring_size_10);
		$criteria->compare('stock_ring_size_11',$this->stock_ring_size_11);
		$criteria->compare('stock_ring_size_12',$this->stock_ring_size_12);
		$criteria->compare('entry_date',$this->entry_date,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}
